package com.shanshan.manage.web.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageInputStreamImpl;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class PdfToPngUtil {
    public static void main(String[] args) throws Exception{
        // Load PDF document
        PDDocument document = PDDocument.load(new File("example.pdf"));

        // Create a PDF renderer
        PDFRenderer renderer = new PDFRenderer(document);

        // Render PDF pages as images
        for (int page = 0; page < document.getNumberOfPages(); page++) {
            BufferedImage image = renderer.renderImageWithDPI(page, 300);

            // Save the image to a PNG file
            File outputFile = new File("image_" + page + ".png");
            ImageIO.write(image, "PNG", outputFile);
        }

        // Close the document
        document.close();
    }
}
